//
//  SYVerifyLoginViewController.m
//  ChatDemo-UI2.0
//
//  Created by zz on 16/8/3.
//  Copyright © 2016年 zz. All rights reserved.
//

#import "SYVerifyLoginViewController.h"
#import "SYModifyPwdViewController.h"

@interface SYVerifyLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *verifyCodeField;
@property (weak, nonatomic) IBOutlet UIButton *getVerifyBtn;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet UILabel *reGetLabel; //重新获取

@property (nonatomic,strong) NSTimer *timer;

@end

@implementation SYVerifyLoginViewController{
    
    int time;//时间，用于倒计时
}

#pragma mark -getter方法
-(NSTimer *)timer{
    
    if (_timer == nil) {
        
        time = 60;
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countdownAction:) userInfo:nil repeats:YES];
    }
    
    return _timer;
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        //让view的y值从64开始计算
        self.edgesForExtendedLayout = UIRectEdgeNone;

    }
    
    return self;
}

- (void)loadView {
    
    [super loadView];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(0, 0, 30, 25)];
    [leftButton setImage:[UIImage imageNamed:@"listmore_arrowleft"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
}

-(void)configureSubViews{
    
    
    //textField
    [_phoneField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    [_verifyCodeField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    //设置获取验证码按钮的相关属性
    self.getVerifyBtn.layer.borderWidth = 0.5;
    self.getVerifyBtn.layer.cornerRadius = 3;
    self.getVerifyBtn.layer.borderColor = [colorFromHex(@"fb524a") CGColor];
    self.getVerifyBtn.enabled = NO;
    
    //重新获取
    _reGetLabel.hidden = YES;
    

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"验证码登录";
    
    //设置相关控件的属性
    [self configureSubViews];
    
    _nextBtn.layer.cornerRadius = 4;
}

- (void)backButtonTapped:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)textFieldDidChange:(UITextField *)textField{
    
    if (textField == _phoneField) {  //是手机号的话
        
        if (textField.text.length > 0) {
            
            _getVerifyBtn.enabled = YES;
            
        }else{
            
            _getVerifyBtn.enabled = NO;
            
        }
        
        [self limitTextFieldLengthWithLength:11 andTextField:textField];
    }
    
    if (textField == _verifyCodeField) { //是验证码的话
        
        
        [self limitTextFieldLengthWithLength:6 andTextField:textField];
    }
    
    if (_phoneField.text.length > 0 && _verifyCodeField.text.length > 0) {
        
        [_nextBtn setBackgroundColor:[UIColor colorWithHexString:@"#fb524a"]];
        _nextBtn.enabled = YES;
        
    }else{
        
        [_nextBtn setBackgroundColor:[UIColor colorWithHexString:@"#fccaca"]];
        _nextBtn.enabled = NO;
    }
}

//限制textField的输入字数,针对非中文输入法限制，使用比较简单的方法
-(void)limitTextFieldLengthWithLength:(NSInteger)length andTextField:(UITextField *)textFIeld{
    
    if (textFIeld.text.length > length) {
        
        textFIeld.text = [textFIeld.text substringToIndex:length];
    }
}

#pragma mark -Action
#pragma mark -获取验证码点击事件
- (IBAction)getVerifyAction:(id)sender {
    
    if (_phoneField.text.length != 11) {
        [Utils alertViewWithMessage:@"请填写正确手机号"];
        return;
    }
    
    //调用获取验证码接口
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:_phoneField.text forKey:@"Mobile"];
    [dict setObject:@"api_VerifyCode_get" forKey:@"method"];
    [dict setObject:@"1" forKey:@"type"];
    
    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        [self.view endEditing:YES];
        
        [self showHint:@"验证码已发送,请注意查收"];
        
        self->_getVerifyBtn.hidden = YES;
        self->_reGetLabel.hidden = NO;
        [self.timer fire];
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
}

#pragma mark -下一步点击事件
- (IBAction)nextBtnClick:(id)sender {
    
    if (_phoneField.text.length != 11) {
        
        [Utils alertViewWithMessage:@"请输入11位手机号码"];
        return;
    }
    
    if (_verifyCodeField.text.length != 6) {
        
        [Utils alertViewWithMessage:@"请输入六位数字验证码"];
        return;
    }
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    
    [dict setObject:@"api_verifycode_verify" forKey:@"method"];
    [dict setObject:_phoneField.text forKey:@"mobile"];
    [dict setObject:_verifyCodeField.text forKey:@"verify"];
    [dict setObject:@"1" forKey:@"type"];
    
    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        if (recode == 0) {
            SYModifyPwdViewController *modifyPwdVC = [[SYModifyPwdViewController alloc] initWithNibName:@"SYModifyPwdViewController" bundle:nil];
            modifyPwdVC.phoneNum = _phoneField.text;
            modifyPwdVC.verifyStr = _verifyCodeField.text;
            [self.navigationController pushViewController:modifyPwdVC animated:YES];
        }
        
    } failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.description];
    }];
    
    
    
}


-(void)countdownAction:(id)sender{
    
    time--;
    _reGetLabel.text = [NSString stringWithFormat:@"重新获取(%d)",time];
    
    if (time == 0 ) {
        
        [_timer invalidate];
        _reGetLabel.hidden = YES;
        _getVerifyBtn.hidden = NO;
        
        _timer = nil;
    }
}

@end
